package shovelsoft.btmetaparser.tokens;

import java.io.IOException;
import java.io.InputStream;

/***
 * Integer is a token representing a number.
 * @author cds
 */
public class Integer implements IToken {

	public final Long mLong;
	
	/***
	 * Constructor: Read an integer, and its terminal 'e' from the input stream.
	 * @param is InputStream.
	 * @throws IOException
	 */
	public Integer(InputStream is) throws IOException {
		
		StringBuilder sb = new StringBuilder();
		int b;
		
		while((b = is.read()) != 'e')
			sb.append((char)b);
		
		mLong = new Long(sb.toString());
	}

	/***
	 * Accept a visit from a token parser (double dispatch)
	 * @param tp Token Parser
	 */
	@Override
	public void accept(ITokenParser tp) throws Exception {
		
		tp.visit(this);
	}

	@Override
	public boolean equals(Object o) {
		
		if(!(o instanceof Integer))
			return false;
		
		Integer i = (Integer)o;
		
		return mLong == i.mLong;
	}
	
	@Override
	public int hashCode() {
		
		return mLong.hashCode();
	}
	
	@Override
	public String toString() {
		
		return "INT \"" + mLong + "\"";
	}
}

